package com.homedo.microservice.open.service.gateway.manager.web.persistence.dao;

import com.homedo.common.dao.BaseDao;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.DbSource;
import com.homedo.microservice.open.service.gateway.manager.web.persistence.mapper.IDbSourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月21日 13时08分00秒
 */
@Repository
public class DbSourceDao extends BaseDao<DbSource> {
    @Autowired
    private IDbSourceMapper dbSourceMapper;

    public List<DbSource> getListByServiceGateway(String serviceGatewayName, List<String> envs) {
        Example example = new Example(DbSource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(DbSource.FIELD_SERVICEGATEWAYNAME, serviceGatewayName);
        if (Detect.notEmpty(envs)) {
            criteria.andIn(DbSource.FIELD_ENV, envs);
        }
        return super.getListByExample(example);
    }

    public void updateByServiceGatewayNameAndEnv(DbSource dbSource) {
        Example example = new Example(DbSource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(DbSource.FIELD_SERVICEGATEWAYNAME, dbSource.getServiceGatewayName());
        criteria.andEqualTo(DbSource.FIELD_ENV, dbSource.getEnv());

        super.updateByExampleSelective(dbSource, example);
    }
}
